package com.gaoyi.basemodule.baseUtil;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.gaoyi.basemodule.R;
import com.gaoyi.basemodule.baseconstant.SPName;

import java.util.Locale;

public class LanguageUtils {
    public static final int LANG_CHINA = 1;
    public static final int LANG_ENGLISH = 2;
    private static volatile LanguageUtils instance;
    private final String LANG_STR_CHINA = "zh-TW,zh";
    private final String LANG_STR_ENGLISH = "en-US,en";
    public static  final String MAP_LANG_CHINA = "zh-TW";
    public static final String MAP_LANG_ENGLISH = "en";
    private Locale currentLocale;
    private String apiLang;
    private String mapLang;

    private LanguageUtils() {
    }

    public static LanguageUtils getInstance() {
        if (null == instance) {
            synchronized (LanguageUtils.class) {
                if (instance == null) {
                    instance = new LanguageUtils();
                }
            }
        }
        return instance;
    }

    public String getEnglishString(){
        return AppUtil.getContext().getString(R.string.english);
    }

    public String getChineseString(){
        return AppUtil.getContext().getString(R.string.chinese);
    }

    public Context change(Locale locale) {
        Context context = AppUtil.getContext();
        Resources resources = context.getApplicationContext().getResources();
        Configuration config = resources.getConfiguration();
        config.setLocale(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocales(new LocaleList(locale));
            context = context.createConfigurationContext(config);
        } else {
            DisplayMetrics dm = resources.getDisplayMetrics();
            resources.updateConfiguration(config, dm);
        }
        AppUtil.initContext(context);
        currentLocale = locale;
        boolean isEnglish = locale == Locale.ENGLISH;
        apiLang = isEnglish ? LANG_STR_ENGLISH : LANG_STR_CHINA;
        mapLang = isEnglish ? MAP_LANG_ENGLISH : MAP_LANG_CHINA;
        return context;
    }


    /**
     * 获取当前语言
     */
    public Locale getCurrentLanguage() {
        if (currentLocale == null) {
            currentLocale = getLanguageInt() == LANG_ENGLISH ? Locale.ENGLISH : Locale.TAIWAN;
        }
        return currentLocale;
    }

    public String getLanguage() {
        if (TextUtils.isEmpty(apiLang)) {
            apiLang = getLanguageInt() == LANG_ENGLISH ? LANG_STR_ENGLISH : LANG_STR_CHINA;
        }
        return apiLang;
    }

    public String getMapLanguage() {
        if (TextUtils.isEmpty(mapLang)) {
            mapLang = getLanguageInt() == LANG_ENGLISH ? MAP_LANG_ENGLISH : MAP_LANG_CHINA;
        }
        return mapLang;
    }

    /**
     * 获取当前语言
     */
    public int getStrLanguage() {
        int language = getLanguageInt();
        if (language == LANG_CHINA) {
            return R.string.chinese;
        } else if (language == LANG_ENGLISH) {
            return R.string.english;
        }
        return R.string.chinese;
    }

    public int getLanguageInt() {
        return SPUtil.getInstance().getInt(SPName.LANGUAGE, LANG_CHINA);
    }

    public boolean isEnglish() {
        return getLanguageInt() == LANG_ENGLISH;
    }

    public Locale getCurLocale() {
        return getLanguageInt() == LANG_ENGLISH ? Locale.ENGLISH : Locale.TAIWAN;
    }

    /**
     * 切换语言时，要重新初始化其他进程的Resources的语言
     */
    public void initResources(Context context) {
        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();
        Locale locale = getCurLocale();
        configuration.setLocale(locale);
        // updateConfiguration
        DisplayMetrics dm = res.getDisplayMetrics();
        res.updateConfiguration(configuration, dm);
    }
}
